package asf.core
{
	import asf.utils.Align;
	
	import com.adobe.utils.StringUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.ui.ContextMenu;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	dynamic internal class Layers extends Proxy
	{
		private var node:XML;
		private var app:Application;
		private var dict:Object;
		private var menu:ContextMenu;
		
		public function Layers( p_node:XML, p_app:Application, contextMenu:ContextMenu = null )
		{
			super( );
			
			node = p_node;
			app = p_app
			dict = new Object( );
			
			menu = contextMenu;
						
			parse( p_node, app.container );
		}
		
		private function parse( baseNode:XML, baseContainer:Sprite ):void
		{
			var layerXML:XML;
			var layers:XMLList = baseNode.layer;
			var layer:Sprite;
			
			for each( layerXML in layers )
			{
				layer = new Layer( );
				layer.name = layerXML.@id || layerXML.id;
				layer.mouseChildren = layer.mouseEnabled = layerXML.@mouseEnabled != "false";
				
				( layer as Layer ).node = layerXML;
				layer.contextMenu = menu;
				
				dict[ layer.name ] = layer;
				
				baseContainer.addChild( layer );
				
				applyAlign( layer, layerXML );
				
				if( layerXML.hasOwnProperty( "layer" ) )
				{
					parse( layerXML, layer );
				}
			}
		}
		
		public function dispose( ):void
		{
			var layer:Sprite;
			var key:String;
			
			for( key in dict )
			{
				layer = dict[ key ];
				
				if( layer.parent )
				{
					layer.parent.removeChild( layer );
				}
				
				delete dict[ key];
			}
			
			dict = null;
			app = null;
			
			menu = null;
			node = null;
		}
		
		private function layerResizeHandler( evt:Event ):void
		{
			var l:Layer = evt.target as Layer;
			
			applyAlign( l, l.node ); 
		}
		
		public function setContextMenu( value:ContextMenu ):void
		{
			var key:String;
			
			menu = value;
			
			for( key in dict )
			{
				( dict[ key ] as Layer ).contextMenu = menu;
			}
		}
		
		private function applyAlign( layer:Sprite, node:XML ):void
		{
			var anchors:uint = 0;
			var align:String = String( node.@align ).toLowerCase( );
			var attr:XMLList = node.attributes( );
			var props:Object = new Object( );
			
			if( StringUtil.trim( align ) != "" )
			{
				if( align.indexOf( "top" ) != -1 ) anchors += Align.TOP;
				if( align.indexOf( "bottom" ) != -1 ) anchors += Align.BOTTOM;
				if( align.indexOf( "middle" ) != -1 ) anchors += Align.MIDDLE;
				if( align.indexOf( "left" ) != -1 ) anchors += Align.LEFT;
				if( align.indexOf( "center" ) != -1 ) anchors += Align.CENTER;
				if( align.indexOf( "right" ) != -1 ) anchors += Align.RIGHT;
				if( align.indexOf( "none" ) != -1 ) anchors += Align.NONE;
				
				if( StringUtil.trim( node.@margin_left ) != "" ) props.margin_left = parseFloat( app.getParsedValueOf( node.@margin_left ) );
				if( StringUtil.trim( node.@margin_right ) != "" ) props.margin_right = parseFloat( app.getParsedValueOf( node.@margin_right ) );
				if( StringUtil.trim( node.@margin_top ) != "" ) props.margin_top = parseFloat( app.getParsedValueOf( node.@margin_top ) );
				if( StringUtil.trim( node.@margin_bottom ) != "" ) props.margin_bottom = parseFloat( app.getParsedValueOf( node.@margin_bottom ) );
				
				if( StringUtil.trim( node.@width ) ) props.width = parseFloat( app.getParsedValueOf( node.@width ) );
				if( StringUtil.trim( node.@height ) ) props.height = parseFloat( app.getParsedValueOf( node.@height ) );
				
				//dtrace( props );
				
				Align.add( layer, anchors, props );
			}
		}
		
		override flash_proxy function callProperty( name:*, ...rest ):* 
		{
			throw new Error( "!WARNING! There is no method called: " + name + " in [Layers]" );
			return null;
		}
		
		override flash_proxy function getProperty( name:* ):*
		{
			return getLayer( name );
		}
		
		public function getLayer( name:String ):Layer
		{
			if( dict[ name ] == null ) 
			{
				throw new Error( "Layer: " + name + " doesn't exist" );
			}
			
			return dict[ name ];
		}
	}
}